package redis;

import java.util.LinkedList;
import java.util.List;

import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import util.RedisCluster;

public class RedisExp {
	public static final String host = "127.0.0.1";
	public static final int port = 6379;

	@Test
	public void pipelineTest() {
		/**
		 * 1. 测试是否在不调用pipe.sync()时，调用response.get会不会抛异常，还是会卡死 结果：
		 * 在不调用pipe.sync函数时，调用response.get函数会抛异常 2.
		 * pipe.sync函数应该是阻塞的，或者说调用时间至少是和发送的命令数成正比的。
		 */
		Jedis jedis = new Jedis(host, 6379);
		Pipeline pipe = jedis.pipelined();
		List<Response<String>> responses = new LinkedList<Response<String>>();
		for (int i = 0; i < 500; i++) {
			responses.add(pipe.set("" + i, "" + i));
		}

		long start = System.currentTimeMillis();
		pipe.sync(); // 这句话是必须的
		System.out.println("time:" + (System.currentTimeMillis() - start));
		responses.clear();
		start = System.currentTimeMillis();
		for (Response<String> res : responses) {
			res.get();
		}
		System.out.println("time:" + (System.currentTimeMillis() - start));

		for (int i = 0; i < 2000; i++) {
			responses.add(pipe.set("" + i, "" + i));
		}

		start = System.currentTimeMillis();
		pipe.sync(); // 这句话是必须的
		System.out.println("time:" + (System.currentTimeMillis() - start));
		responses.clear();
		start = System.currentTimeMillis();
		for (Response<String> res : responses) {
			res.get();
		}
		System.out.println("time:" + (System.currentTimeMillis() - start));

		for (int i = 0; i < 10; i++) {
			jedis.del("" + i);
		}
	}

	@Test
	public void redisPipeline() {
		/**
		 * 测试结果： 每次调用redis.pipeline()，只会生成一个新的pipeline，并且之前的pipeline不会被关掉
		 */
		Jedis redis = new Jedis(host, port);
		Pipeline a = redis.pipelined();
		Response<String> ret = a.set("a", "b");
		Pipeline b = redis.pipelined();
		ret.get(); // 测试是否新new一个pipeline，之前的pipeline会被关掉
		if (a == b) {
			System.out.println("the same");
		}
	}

	@Test
	public void pipelineAndGet() {
		/**
		 * 测试pipeline和get是否能够一起调用
		 */

		Jedis redis = new Jedis(host, port);
		Pipeline a = redis.pipelined();
		a.del("a");
		a.del("c");
		a.del("b");
		a.sync();
		Response<Long> ret = a.sadd("a", "b");
		a.sync();
		redis.smembers("a"); // sync之后是可以使用jedis的接口的
		ret = a.sadd("a", "c");// pipeline是可以重用的
		a.hset("b", "b", "b");
		a.sync();
		System.out.println(redis.smembers("a"));

		a.sadd("c","c");
		System.out.println(redis.hget("b","b")); // 没sync之前也可以调用redis的接口
		a.sync();
	}

	@Test
	public void localRedisPerform() {
		RedisCluster cluster = new RedisCluster(new String[] { "10.11.1.61" },
				port);

		long start = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			String key = cluster.getJedis(0).randomKey();
			cluster.getJedis(0).smembers(key);
		}
		long end = System.currentTimeMillis();
		System.out.println((end - start) / 10000.0 + "ms");
	}

	@Test
	public void hgetNoExistKeyTest() {
		RedisCluster cluster = new RedisCluster(new String[] { "127.0.0.1" },
				port);

		cluster.getJedis(0).hset("non", "asdf", "sdf");
		cluster.getJedis(0).hget("non", "non");
	}
}
